package com.janko.redislock.io;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.janko.redislock.io.entity.EAPModel;
import org.springframework.util.ObjectUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author nyk
 * @Date 2021/4/15 11:15
 * @Version 1.0
 * @Desc
 */
public class SomeTest {


    public static void main(String[] args) {
        Map<String, List<String>> map = new HashMap<>();
        addUserId(map, "1", "create");
        addUserId(map, "1", "create");
        addUserId(map, "1", "create");
        System.out.println(map.get("create"));

        Map<String, Object> map2 = new HashMap<>();
        map2.put("ruleCode", "1_1_sdfjashdfjkh2euihksj");
        map2.put("ruleAttribute", map);
        System.out.println(map2Json(map2));
    }


    public static String map2Json(Map<String, Object> map){
        ObjectMapper objectMapper = new ObjectMapper();
        String json = "";
        try {
            json = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }

    private static void addUserId(Map<String, List<String>> userMap, String userId, String column) {
        if (ObjectUtils.isEmpty(userMap.get(column))) {
            userMap.put(column, Arrays.asList(userId.split(",")));
        }else{
            List<String> list=new ArrayList<>(userMap.get(column));
            list.add(userId);
            userMap.put(column,list);
        }
    }

    static String readFile(String filePath) {
        filePath = "D:\\" + filePath;
        StringBuilder sb=new StringBuilder();
        try {
            String encoding="UTF-8";
            File file=new File(filePath);
            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null){
                    sb.append(lineTxt);
//                    System.out.println(lineTxt);
                }
                read.close();
            }else{
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return sb.toString();
    }
}
